/*
 * Agenda.java
 *
 * Created on 2007��9��30��, ����10:34
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.microweb.toastmaster.model;

import com.microweb.toastmaster.Constants;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 
 * @author zhengyd
 * 
 * @hibernate.class table="agenda"
 */
public class Agenda extends BaseObject implements Serializable {
	private final Log log = LogFactory.getLog(Agenda.class);

	protected Long id;
	protected String venue;
	protected String theme;
	protected String minutes;
	protected Phrase phrase = new Phrase();
	protected Date date;
	protected Set<Actor> actors = new HashSet<Actor>();

	/** Creates a new instance of Agenda */
	public Agenda() {
	}

	/**
	 * @hibernate.id column="id" generator-class="native" unsaved-value="null"
	 */
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * @hibernate.property length="50"
	 */
	public String getVenue() {
		return this.venue;
	}

	public void setVenue(String venue) {
		this.venue = venue;
	}

	/**
	 * @hibernate.property length="200"
	 */
	public String getTheme() {
		return this.theme;
	}

	public void setTheme(String theme) {
		this.theme = theme;
	}

	/**
	 * @hibernate.property column="minutes" length="2000" not-null="false"
	 */
	public String getMinutes() {
		return this.minutes;
	}

	public void setMinutes(String minutes) {
		this.minutes = minutes;
	}

	/**
	 * @hibernate.component
	 */
	public Phrase getPhrase() {
		return phrase;
	}

	public void setPhrase(Phrase phrase) {
		this.phrase = phrase;
	}

	/**
	 * @hibernate.property column="date"
	 */

	public Date getDate() {
		return this.date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	/**
	 * @hibernate.set name="actors" lazy="false" inverse="true" cascade="all"
	 * @hibernate.key column="agenda_id"
	 * @hibernate.one-to-many class="com.microweb.toastmaster.model.Actor"
	 * 
	 * 
	 */
	public Set<Actor> getActors() {
		return this.actors;
	}

	public void setActors(Set<Actor> actors) {
		this.actors = actors;
	}

	public String getToastMaster() {
		return getUsernameByRolename(Constants.ROLENAME_TOASTERMASTER);
	}

	public String getUsernameByRolename(String rolename) {
		String username = null;

		Iterator<Actor> it = actors.iterator();
		while (it.hasNext()) {
			Actor actor = (Actor) it.next();
			String name = actor.getTMRole().getName();
			if (rolename.equalsIgnoreCase(name)) {
				username = actor.getUser().getFullName();
				return username;
			}
		}

		return username;
	}

	public User getUserByRolename(String rolename) {
		User user = null;

		Iterator<Actor> it = actors.iterator();
		while (it.hasNext()) {
			Actor actor = (Actor) it.next();
			String name = actor.getTMRole().getName();
			if (rolename.equalsIgnoreCase(name)) {
				user = actor.getUser();
				return user;
			}
		}

		return user;
	}

	public boolean equals(Object o) {
		if (this == o)
			return true;
		if (!(o instanceof Agenda))
			return false;

		final Agenda agenda = (Agenda) o;

		if (id != null ? !id.equals(agenda.getId()) : agenda.getId() != null)
			return false;

		return true;
	}

	public int hashCode() {
		return (id != null ? id.toString().hashCode() : 0);
	}

	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append(" Agenda{");

		if (this.id != null)
			sb.append(this.id.toString() + "=>id,");

		sb.append("Actors<");
		Iterator<Actor> it = actors.iterator();
		while (it.hasNext()) {
			sb.append((Actor) it.next() + " | ");
		}
		sb.append("> }");

		return sb.toString();
	}

	public Actor deleteActorByRolename(String rolename) {
		if (rolename == null || rolename.trim().length() == 0)
			return null;

		Iterator<Actor> it = actors.iterator();
		Actor actor = null;
		boolean found = false;
		while (it.hasNext()) {
			actor = (Actor) it.next();
			if (rolename.equals(actor.getTMRole().getName())) {
				found = true;
				break;
			}
		}

		if (found) {
			actor.setAgenda(null);
			actors.remove(actor);
			log.debug("removed an actor: " + rolename);
			return actor;
		}

		return null;
	}

	public Actor getActorByRolename(String rolename) {
		if (rolename == null || rolename.trim().length() == 0)
			return null;

		Iterator<Actor> it = actors.iterator();
		Actor actor = null;
		boolean found = false;
		while (it.hasNext()) {
			actor = (Actor) it.next();
			if (rolename.equals(actor.getTMRole().getName())) {
				found = true;
				break;
			}
		}

		if (found) {
			return actor;
		}

		return null;
	}

	public List<Actor> getActorsByRolename(String rolename) {
		List<Actor> results = new ArrayList<Actor>();
		Iterator<Actor> it = actors.iterator();
		while (it.hasNext()) {
			Actor actor = (Actor) it.next();
			if (actor.getTMRole().getName().contains(rolename))
				results.add(actor);
		}

		return results;
	}

	public List<Actor> getActorsByUsername(String username) {
		List<Actor> results = new ArrayList<Actor>();
		if (username == null || username.trim().length() == 0)
			return results;

		Iterator<Actor> it = actors.iterator();
		while (it.hasNext()) {
			Actor temp = (Actor) it.next();
			if (username.equals(temp.getUser().getUsername())) {
				results.add(temp);
			}
		}

		return results;
	}

	public Actor getActorByRoleAndUser(String rolename, String username) {
		if (rolename == null || rolename.trim().length() == 0)
			return null;

		if (username == null || username.trim().length() == 0)
			return null;

		Iterator<Actor> it = actors.iterator();
		while (it.hasNext()) {
			Actor actor = (Actor) it.next();
			if (rolename.equals(actor.getTMRole().getName())
					&& username.equals(actor.getUser().getUsername())) {
				return actor;
			}
		}

		return null;
	}

	public List<Actor> getActorsByRoleAndUser(String rolename, String username) {
		List<Actor> results = new ArrayList<Actor>();
		if (rolename == null || rolename.trim().length() == 0)
			return results;

		if (username == null || username.trim().length() == 0)
			return results;

		Iterator<Actor> it = actors.iterator();
		while (it.hasNext()) {
			Actor actor = (Actor) it.next();

			if (actor.getTMRole().getName().contains(rolename)
					&& username.equals(actor.getUser().getUsername())) {
				results.add(actor);
			}
		}

		return results;
	}

	public Actor addOrUpdateActor(TMRole tmrole, User user) {
		if (tmrole == null || user == null)
			return null;

		log.debug("input role: " + tmrole.getName() + " input user: "
				+ user.getUsername());
		Actor actor = getActorByRolename(tmrole.getName());
		if (actor == null) {
			actor = new Actor();
			actor.setTMRole(tmrole);
			actor.setAgenda(this);
			actors.add(actor);
			log.debug("add a new actor.");

		} else {
			log.debug("update old actor");
		}

		actor.setUser(user);
		log.debug("actor number = " + actors.size());
		return actor;
	}

	public String getUsernameByRole(String rolename) {
		String username = "";
		Actor actor = this.getActorByRolename(rolename);
		if (actor != null)
			username = actor.getUser().getFullName();

		return username;
	}

	public Speech getSpeech(Integer index) {
		Speech speech = null;
		Actor actor = this.getActorByRolename(Constants.ROLENAME_SPEAKER
				+ index.toString());

		if (actor == null)
			return speech;

		User speaker = actor.getUser();
		Set<?> speeches = speaker.getSpeeches();
		log.debug("speaker: " + speaker.getUsername() + " has "
				+ speeches.size() + " speeches.");
		Iterator<?> it = speeches.iterator();
		log.debug("agenda date: " + this.date);
		while (it.hasNext()) {
			Speech temp = (Speech) it.next();
			log.debug("speech date: " + temp.getDate());

			if (temp.getDate() == null)
				continue;

			if (temp.getDate().equals(this.getDate())) {
				speech = temp;
				break;
			}
		}

		return speech;
	}

}
